package com.china08.yxyapi.service.mg.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.mg.mistakes.StuMistakesQuesWeike;
import com.china08.yxyapi.repository.mg.mistakes.StuMistakesQuesWeikeRepository;
import com.china08.yxyapi.service.mg.StuVipWeikeViewService;

@Service
public class StuVipWeikeViewServiceImpl implements StuVipWeikeViewService {
    @Autowired
    private StuMistakesQuesWeikeRepository stuMistakesQuesWeikeRepository;

    /**
     * 免费次数
     */
    private static final Integer FREE_TIMES = 3;

    @Override
    public Integer stuVipValidate(String stuId) {
        int count = FREE_TIMES;
        // 如果非vip,查询可以免费看次数
        StuMistakesQuesWeike sqw = stuMistakesQuesWeikeRepository.findById(stuId);
        if (sqw == null) {
            sqw = new StuMistakesQuesWeike();
            sqw.setId(stuId);
            sqw.setCreatedBy(stuId);
            sqw.setStudentId(stuId);
            sqw.setTimes(FREE_TIMES - 1);
            sqw.setLastModifiedBy(stuId);
            count = FREE_TIMES;
        } else {
            count = sqw.getTimes();
            if (count != 0) {
                sqw.setTimes(sqw.getTimes() - 1);
            }
        }
        stuMistakesQuesWeikeRepository.save(sqw);
        return count;
    }

}
